constructive algorithms greedy

Please click on ads to support us..

Python Code:

num = int(input())

for i in range(num):
    data = input()
    data = data.split(" ")
    n = int(data[0])
    k = int(data[1])
    r = int(data[2])
    c = int(data[3])
    
    fil = []
    ans = []
    if k == 1:
        for z in range(n):
            row = "X"*n
            ans.append(row)
        print("\n".join(ans))
            
                
    else:
        isTrues = True
        for z in range(k):
            row = ""
            
            if (z + 1 == r % k or (r % (z+1) == 0 and z+1 == k)) and isTrues:
                isTrues = False
                for j in range(k):
                    if (j+1 == c % k or (c % (j+1) == 0 and j+1 == k)):
                        row += "X"
                    else:
                        row += "."
            else:
                isTrue = True
                for j in range(k):
                    if j+1 != c % k and j+1 not in fil and isTrue:
                        fil.append(j+1)
                        row += "X"
                        isTrue = False
                    else:
                        row += "."
            row = row*int(n/k)
            ans.append(row)
    		
        ans = ans*int(n/k)
    	
        print("\n".join(ans))
	
	
	

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define llu unsigned long long
#define endl "\n"
#define inf 0x3f3f3f3f

using namespace std;

typedef pair<int, int> PII;

const int N = 1e6 + 7;

void solve()
{
    int n,k,r,c;
    cin>>n>>k>>r>>c;
    int x = n/k;
    int y = (c-(r%k)+1+k)%k;//初始指针
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(j%k==y||(y%k==0&&j%k==0)){
                cout<<"X";
            }else{
                cout<<".";
            }
        }
        ++y;
        if(y>k){
            y%=k;
        }
        cout<<'\n';
    }
}

signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments